Warn about duplicated build targets
authorPanashe M. Fundira <fundirap@gmail.com>
Sun, 10 Jul 2016 17:29:31 +0000 (13:29 -0400)
committerPanashe M. Fundira <fundirap@gmail.com>
Wed, 3 Aug 2016 14:27:33 +0000 (10:27 -0400)
src/cargo/util/toml.rs
tests/build.rs

index 158fbdc225b00830dbcce605c3155d8c7799c578..5f35e3dd6e143fb51f9e75ba323dcba37820873d 100644 (file)
@@ -535,6 +535,10 @@ impl TomlManifest {
             debug!("manifest has no build targets");
         }
 
+        if let Err(e) = unique_build_targets(&targets, layout) {
+            bail!("duplicate build target found: `{}`", e);
+        }
+
         let mut deps = Vec::new();
         let replace;
 
@@ -752,6 +756,18 @@ fn unique_names_in_targets(targets: &[TomlTarget]) -> Result<(), String> {
     Ok(())
 }
 
+/// Will check a list of build targets, and make sure the target names are unique within a vector.
+/// If not, the name of the offending build target is returned.
+fn unique_build_targets(targets: &[Target], layout: &Layout) -> Result<(), String> {
+    let mut seen = HashSet::new();
+    for v in targets.iter().map(|e| layout.root.join(e.src_path())) {
+        if !seen.insert(v.clone()) {
+            return Err(v.display().to_string());
+        }
+    }
+    Ok(())
+}
+
 impl TomlDependency {
     fn to_dependency(&self,
                      name: &str,
index c45d50660c6ea2ead2f01b56886eddcfc14077ff..5f2f67913d3212ca376e9ec175fb112c62dc9043 100644 (file)
@@ -101,6 +101,39 @@ Caused by:
 src[..]Cargo.toml:1:5-1:6 expected a value\n\n"))
 }
 
+#[test]
+fn cargo_compile_duplicate_build_targets() {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "foo"
+            version = "0.0.1"
+            authors = []
+
+            [lib]
+            name = "main"
+            crate-type = ["dylib"]
+
+            [dependencies]
+        "#)
+        .file("src/main.rs", r#"
+            fn main() {}
+        "#);
+
+    let error_message = format!("\
+[ERROR] failed to parse manifest at `[..]`
+
+Caused by:
+  duplicate build target found: `{}`",
+                     p.root().join("src[..]main.rs").display());
+
+    assert_that(p.cargo_process("build"),
+                execs()
+                .with_status(101)
+                .with_stderr(error_message)
+    )
+}
+
 #[test]
 fn cargo_compile_with_invalid_version() {
     let p = project("foo")